import sys
from string import *
from player import *

class Team:

  # This is the constructor method for a team.
  #
  # **Arguments**
  #
  # *name* a string containing the name of the team
  #
  # *datafile* the file containing player data
  #
  # **returns** none
  def __init__(self,whererun,name,datafile):

    # Where is this being run (prompt or cgi)?
    self.whererun = whererun

    # Strip off any blanks from team name
    self.name = strip(name)

    # Set up a list of players
    self.players = []

    # Call the read_data routine to fill the player list
    self.Readdata(datafile)
    # Adjust the lineup
    self.Set_lineup()

    # Set up some team statistics
    self.upnext=0
    self.hits = 0
    self.errs = 0
    self.scorebyinning = []

  # This function parses the datafile.
  # Note: this program reads the standard USAToday baseball stats
  #
  # **Arguments**
  #
  # *datafile* - string containing the name of the file to read
  #
  # **returns** none
  def Readdata(self,datafile):
    teamfile=open(datafile,'r')
    header = teamfile.readline()
    rest_of_data = teamfile.readlines()
    for dataline in rest_of_data:
      # strip blank space off a line of data
      if strip(dataline):
        # create a player with the given data and append to players list
        #
        # Note: this dataline must be in the format of the 
        # standard USAToday baseball stats
        self.players.append(Player(dataline))      

  # This function gets a new batter
  #
  # **Arguments** - none
  #
  # **returns** none
  def Newbatter(self):
    self.upnext=self.upnext+1
    if self.upnext == 9:
      self.upnext=0

  # This function lets you change the batting order
  #
  # **Arguments** - none
  #
  # **returns** none
  def Set_lineup(self):
    choice = "y" 
    if self.whererun=="main":
      choice = raw_input("Use default lineup for "+self.name+"? (y) ")
    if choice == "n":
      print "Your choices are: "
      for n in self.players:
        print n.name
      print
      for k in range(1,10):
        print "Who do you want to bat number ",k,"? (",self.players[k-1].name,")",
        choice2=raw_input()
        if choice2: 
          num=0
          temp=self.players[k-1]
          for n in self.players:
            if n.name==choice2:
              self.players[k-1]=n
              self.players[num]=temp
            num=num+1
            
